import axios from 'axios';
import {
    Message
} from 'element-ui';
import store from '@/store';
import router from '@/router';
export const base_url = process.env.NODE_ENV === "development" ? 'http://localhost:8990/api' : 'http://112.124.19.75:8870/api'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
const instance = axios.create({
    baseURL: base_url,
    timeout: 8000,
})


instance.interceptors.request.use(config => {
    config.headers['Authorization'] = store.state.userModule.token
    return config;
}, err => {
    console.log('发送请求失败' + err);
    Promise.reject()
});

instance.interceptors.response.use(res => {
    const {
        code,
        msg
    } = res.data;
    if (code !== 200) {
        if (code === 403) {
            Message.error(msg + ",请重新登录")
            store.dispatch('LogoutSuccess')
            router.push("/login")
        } else {
            Message.error(msg)
        }
    }
    return res.data
}, err => {
    console.log("err" + err);
    Message.error(err.message)
    return Promise.reject(err);
});

export default instance;